# @Time : 2020/12/10 10:31
# @Author: sl
# @File : userInsert.py
import traceback

from flask import request
from jsonschema import validate
from app.v2.models.UserModel import UserSample
from framework.api.AbstractApi import AbstractApi
from framework import db

'''
 get 单表请求示例 保存名字后，返回带主键ID的整个对象 http://127.0.0.1:8181/v2/userInsert?name=2222&number=2
'''

my_schema = {
    "type": "object",
    # "properties": {
    #     "number": {
    #         "description": "编号必须数字",
    #         "type": "integer"
    #     }
    # },
    "required": [
        "name", "number"
    ]
}
#成对出现

class UserInsert(AbstractApi):
    def handle_get_request(self):
        validate(instance=request.args, schema=my_schema)  # 校验数据字段必填
        user = UserSample()
        user.__dict__.update(request.args)
        db.session.add(user)
        db.session.flush()  # 获取id要用
        return user
